package edu.maping.easy.model;

import java.awt.Frame;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;

import codeanticode.glgraphics.GLConstants;

import processing.core.PApplet;
import processing.core.PFont;
import processing.core.PImage;
import processing.xml.XMLElement;
import processing.xml.XMLWriter;
import controlP5.Bang;
import controlP5.Button;
import controlP5.ControlEvent;
import controlP5.ControlGroup;
import edu.maping.easy.ControlManager;
import edu.maping.easy.EasyMapping;
import edu.maping.easy.composer.Composer;
import edu.maping.easy.editor.EasyMappingEditor;
import edu.maping.easy.fisica.EasyMappingFPoly;
import edu.maping.easy.fisica.EasyMappingFisica;
import edu.maping.easy.gui.CircleButton;
import edu.maping.easy.gui.GUIEvent;
import edu.maping.easy.gui.GuiFactory;
import edu.maping.easy.gui.ScreenLogger;
import edu.maping.easy.gui.Slider;
import edu.maping.easy.io.KeyBoardInteraction;
import edu.maping.easy.minim.MinimPlugin;
import edu.maping.easy.painter.PainterDelegateEditor;
import edu.maping.easy.painter.PolygonRendererManager;
import edu.maping.easy.painter.plugins.FireCube;
import edu.maping.easy.painter.plugins.FisicaRenderer;
import edu.maping.easy.painter.plugins.FotoViewer;
import edu.maping.easy.painter.plugins.ThingPainterContournTransform;
import edu.maping.easy.painter.plugins.ThingPainterRectangleTransform;
import edu.maping.easy.painter.plugins.ThingPainterRectangleTransform3D;
import edu.maping.easy.sequence.PropagationManager;
import edu.maping.easy.sequence.Sequencer;
import edu.maping.easy.sequence.ticker.CollisionTicker;
import edu.maping.easy.sequence.ticker.PropagationTicker;
import edu.maping.easy.sequence.ticker.TimeTicker;
import fisica.FBody;
import fisica.FContact;

public class PolygonsByClickSecond extends PApplet {

	/**
	 * Polygons
	 * 
	 * by Ricard Marxer
	 * 
	 * This example shows how to create polygon bodies.
	 */

	// boolean editDraw = false;
	// World world = new World();
	//
	// int panelX = 0;
	// int panelY = 15;
	//
	// PImage fondo = null;
	//
	// ArrayList<EasyMappingPlugin> plugins = new
	// ArrayList<EasyMappingPlugin>();
	// ArrayList<MouseInteraction> mouseInteractions = new
	// ArrayList<MouseInteraction>();
	// ArrayList<GUIInteraction> controlP5Interactions = new
	// ArrayList<GUIInteraction>();
	//
	// EasyMappingFisica fisicaPlugin = null;
	// ControlManager controlManager = null;
	// EasyMappingEditor easyMappingEditor = null;
	// GuiFactory guiFactory = null;
	//
	// PolygonRendererManager painterManager = new PolygonRendererManager();
	//
	// ScreenLogger screenLogger = null;
	//
	// Sequencer sequencer = null;
	// KeyBoardInteraction keyBoardInteraction = new KeyBoardInteraction();
	//
	// PImage pImage = null;
	//
	// PImage pImage1 = null;
	// PImage pImage2 = null;

	public void setup() {

		size(1024, 768, GLConstants.GLGRAPHICS);
		hint(ENABLE_OPENGL_4X_SMOOTH);
		hint(ENABLE_ACCURATE_TEXTURES);
		hint(ENABLE_NATIVE_FONTS);
		//smooth();

		// pImage1 = createImage(width , height, RGB);
		// pImage2 = createImage(width / 2, height, RGB);

		// hint(ENABLE_DEPTH_SORT);

		// fondo = createImage(width, height, ALPHA);

		EasyMapping easyMapping = new EasyMapping(this);
//		easyMapping.createEmbeddedNApplet(
//				"edu.maping.easy.painter.plugins.BouncyBubbles",
//				100, 100);
		frameRate(30);

		// rectMode(CORNERS);
		//
		// /* CONSTRUCCION DE PLUGINS */
		// try {
		// PFont font = loadFont("Ziggurat-HTF-Black-32.vlw");
		// textFont(font, 9);
		// controlManager = initControlPAnel();
		// MouseWheelInput wheelInput = initMouseWheelListener();
		// initPlugins(wheelInput);
		// addPainterDelegates();
		// initControlPanelNew();
		// } catch (Exception e) {
		// e.printStackTrace();
		// }
	}

	public void draw() {

	}

	// private MouseWheelInput initMouseWheelListener() {
	// MouseWheelInput mouseWheelInput = new MouseWheelInput();
	// frame.addMouseWheelListener(mouseWheelInput);
	// return mouseWheelInput;
	// }
	//
	// private void initPlugins(MouseWheelInput mouseWheelInput) {
	// // controlManager = initControlPAnel();
	//
	// guiFactory = new GuiFactory();
	// guiFactory.setup(this);
	//
	// PainterDelegateEditor painterDelegateEditor = new
	// PainterDelegateEditor();
	// painterDelegateEditor.guiFactory = guiFactory;
	//
	// MinimPlugin minimPlugin = new MinimPlugin();
	// minimPlugin.setup(this);
	// plugins.add(minimPlugin);
	//
	// // fisicaPlugin = new EasyMappingFisica();
	// // fisicaPlugin.setup(this, guiFactory);
	// // fisicaPlugin.fftFacade = minimPlugin;
	// //plugins.add(fisicaPlugin);
	// // controlP5Interactions.add(fisicaPlugin);
	//
	// /* CONSTRUCCIÓN DE DELEGADOS DE RENDERIZADO DE POLIGONOS */
	// sequencer = new Sequencer();
	// sequencer.setup(this, world, controlManager, guiFactory);
	// sequencer.painterDelegateEditor = painterDelegateEditor;
	//
	// plugins.add(sequencer);
	//
	// controlP5Interactions.add(sequencer);
	// sequencer.rendererManager = painterManager;
	// mouseWheelInput.list.add(sequencer);
	//
	// minimPlugin.setTimeTicker(sequencer);
	//
	// Composer composer = new Composer();
	// composer.sequencer = sequencer;
	// mouseWheelInput.list.add(composer);
	//
	// plugins.add(composer);
	//
	// controlP5Interactions.add(composer);
	// composer.setup(this, guiFactory);
	//
	// screenLogger = new ScreenLogger();
	// screenLogger.setup(this);
	//
	// easyMappingEditor = new EasyMappingEditor();
	// easyMappingEditor.screenLogger = screenLogger;
	// easyMappingEditor.setup(this, world, guiFactory);
	// plugins.add(easyMappingEditor);
	// controlP5Interactions.add(easyMappingEditor);
	//
	// sequencer.screenLogger = screenLogger;
	//
	// plugins.add(guiFactory);
	//
	// mouseInteractions.add(painterDelegateEditor);
	// //mouseInteractions.add(fisicaPlugin);
	// mouseInteractions.add(sequencer);
	// mouseInteractions.add(easyMappingEditor);
	// mouseInteractions.add(guiFactory);
	//
	// PropagationManager propagationManager = new PropagationManager();
	// propagationManager.setGuiFactory(guiFactory);
	// propagationManager.setScreenLogger(screenLogger);
	// propagationManager.setEasyMappingEditor(easyMappingEditor);
	//
	// propagationManager.setup();
	// painterManager.propagationManager = propagationManager;
	//
	// }
	//
	// private void addPainterDelegates() {
	//
	// ThingPainterContournTransform contourn = new
	// ThingPainterContournTransform();
	// ThingPainterRectangleTransform test = new
	// ThingPainterRectangleTransform();
	// ThingPainterRectangleTransform3D test2 = new
	// ThingPainterRectangleTransform3D();
	// FotoViewer fotoViewer = new FotoViewer();
	// FireCube fireCube = new FireCube();
	// FisicaRenderer fisicaRenderer = new FisicaRenderer();
	//
	// painterManager.setup(this, world, controlManager, guiFactory);
	//
	// painterManager.addPainter(contourn);
	// painterManager.addPainter(test);
	// painterManager.addPainter(test2);
	// painterManager.addPainter(fotoViewer);
	// painterManager.addPainter(fireCube);
	// painterManager.addPainter(fisicaRenderer);
	//
	//
	// TimeTicker timeTicker = new TimeTicker();
	// CollisionTicker collisionTicker = new CollisionTicker();
	// collisionTicker.fisica = fisicaPlugin;
	//
	// painterManager.add(timeTicker);
	// painterManager.add(collisionTicker);
	//
	// }
	//
	// private ControlManager initControlPAnel() {
	//
	// ControlManager controlP5 = new ControlManager(this);
	// ControlGroup l = controlP5.getGroup();
	// controlP5.setMainGroup(l);
	//
	// panelX = width - 120;
	//
	// // Button bang = controlP5.addButton("startPolygon", 0, width - 100,
	// // panelY + 10, 20, 20);
	// // bang.setLabel("normal");
	// // bang.setGroup(l);
	// //
	// // Button bang2 = controlP5.addButton("startNewPhysic", 0, width - 60,
	// // 10,
	// // 20, 20);
	// // bang2.setLabel("fisica");
	// // bang2.setGroup(l);
	//
	// // controlP5.addButton("startVisual", 0, 30, 10, 20,
	// // 20).setLabel("Start");
	// // controlP5.addButton("endVisual", 0, 60, 10, 20, 20).setLabel("End");
	//
	// // controlP5.addButton("loadFromFile", 0, 100, 10, 20, 20)
	// // .setLabel("Load");
	// // controlP5.addButton("saveToFile", 0, 130, 10, 20,
	// // 20).setLabel("Save");
	//
	// Bang bang3 = controlP5.addBang("delete", 0, 145, 20, 20);
	// bang3.setLabel("DELETE");
	// bang3.setGroup(l);
	//
	// return controlP5;
	// }
	//
	// private void initControlPanelNew() {
	// edu.maping.easy.gui.Button startVisual = new edu.maping.easy.gui.Button(
	// "Start", 30, 10, 50, 20, this);
	// guiFactory.add(startVisual);
	//
	// edu.maping.easy.gui.Button endVisual = new edu.maping.easy.gui.Button(
	// "End", 80, 10, 40, 20, this);
	// guiFactory.add(endVisual);
	//
	// edu.maping.easy.gui.Button load = new edu.maping.easy.gui.Button(
	// "Load", 140, 10, 40, 20, this);
	// guiFactory.add(load);
	//
	// edu.maping.easy.gui.Button save = new edu.maping.easy.gui.Button(
	// "Save", 185, 10, 40, 20, this);
	// guiFactory.add(save);
	// }
	//
	// public void draw() {
	//
	// try {
	//
	// controlManager.getMainGroup().setPosition(width - 100, panelY + 10);
	// controlManager.getMainGroup().updateAbsolutePosition();
	//
	// background(0);
	// controlManager.draw();
	// if (editDraw) {
	// world.editDraw(this);
	// }
	//
	// for (EasyMappingPlugin plugin : plugins) {
	// plugin.draw(this);
	// }
	//
	// screenLogger.draw(this);
	//
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	//
	// }

	// public void saveToFile() {
	//
	// try {
	//
	// XMLElement father = new XMLElement();
	// father.setName("EasyMappingWorld");
	//
	// for (Thing thing : world.world) {
	// father.addChild(thing.startXmlization());
	// }
	//
	// XMLElement seqElement = sequencer.xmlIzation();
	// father.addChild(seqElement);
	//
	// String file = selectOutput();
	// if (file == null) {
	// return;
	// }
	// PrintWriter printWriter = createWriter(file);
	// XMLWriter writer = new XMLWriter(printWriter);
	//
	// try {
	// writer.write(father);
	// } catch (IOException e) {
	// e.printStackTrace();
	// }
	//
	// printWriter.flush();
	// printWriter.close();
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// }
	//
	// public void loadFromFile() {
	// String file = selectInput();
	// if (file == null)
	// return;
	// BufferedReader reader = createReader(file);
	// XMLElement xmlElement = new XMLElement(reader);
	// String name = xmlElement.getName();
	// // System.out.println("Leido fichero " + name);
	// for (int index = 0; index < xmlElement.getChildCount(); index++) {
	// XMLElement child = xmlElement.getChild(index);
	//
	// if (child.getName().equals(Thing.XML_NAME)) {
	// PolygonThing polygonThing = new PolygonThing();
	// polygonThing = polygonThing.startDesXmlization(child);
	// // addPainterDelegates(polygonThing);
	// world.world.add(polygonThing);
	// }
	//
	// if (child.getName().equals("Sequences")) {
	// try {
	// sequencer.desXmlIzation(child);
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// }
	// // if(child.getName().equals(anObject))
	// }
	// }

	// public void mousePressed() {
	//
	// for (MouseInteraction plugin : mouseInteractions) {
	// plugin.mousePreassed(mouseX, mouseY,
	// easyMappingEditor.getNewThing());
	// }
	// }

	public void contactStarted(FContact c) {
		// fisicaPlugin.startCollision(c.getBody1(), c.getBody2());
	}

	public void contactEnded(FContact c) {
		// fisicaPlugin.endCollision(c.getBody1(), c.getBody2());
	}

	// public void mouseDragged() {
	//
	// for (MouseInteraction plugin : mouseInteractions) {
	// plugin.mouseDragged(mouseX, mouseY, easyMappingEditor.getNewThing());
	// }
	//
	// FBody clicked = guiFactory.getHovered();
	//
	// if (clicked == null || !clicked.isDrawable()) {
	// return;
	// }
	//
	// if (clicked instanceof Slider) {
	// Slider slider = (Slider) clicked;
	// slider.setValue(mouseX, mouseY);
	//
	// GUIEvent event = new GUIEvent();
	// event.slider = slider;
	// propagateEvent(event);
	// }
	//
	// guiFactory.setClicked(null);
	//
	// }

	// @Override
	// public void mouseMoved() {
	// // TODO Auto-generated method stub
	// for (MouseInteraction plugin : mouseInteractions) {
	// plugin.mouseMoved(mouseX, mouseY);
	// }
	// }

	//
	// //
	// public void mouseReleased() {
	//
	// GUIEvent event = new GUIEvent();
	//
	// guiFactory.setClicked(null);
	//
	// for (MouseInteraction plugin : mouseInteractions) {
	// plugin.mouseReleased(mouseX, mouseY, world.hovered(mouseX, mouseY));
	// }
	//
	// FBody clicked = guiFactory.getHovered(mouseX, mouseY);
	// if (clicked != null && clicked.isDrawable()) {
	// if (clicked instanceof edu.maping.easy.gui.Button) {
	// edu.maping.easy.gui.Button button = (edu.maping.easy.gui.Button) clicked;
	// event.button = button;
	// if (button.name != null && button.name.equals("Start")) {
	// startVisual();
	// } else if (button.name != null && button.name.equals("End")) {
	// endVisual();
	// } else if (button.name != null && button.name.equals("Load")) {
	// loadFromFile();
	// } else if (button.name != null && button.name.equals("Save")) {
	// saveToFile();
	// } else {
	// System.out.println("ERRO");
	// }
	// }
	//
	// if (clicked instanceof Slider) {
	// Slider slider = (Slider) clicked;
	// slider.setValue(mouseX, mouseY);
	// event.slider = slider;
	// }
	//
	// if (clicked instanceof CircleButton) {
	// CircleButton circleButton = (CircleButton) clicked;
	// event.circleButton = circleButton;
	// }
	//
	// propagateEvent(event);
	// }
	//
	// guiFactory.setClicked(null);
	// }

	// private void propagateEvent(GUIEvent event) {
	// for (GUIInteraction plugin : controlP5Interactions) {
	//
	// try {
	// plugin.controlEvent(event);
	// } catch (Exception e) {
	// e.printStackTrace();
	// System.out.println("ERROR EN EL PLUGIN " + plugin.toString());
	// }
	// }
	// }

	// public void mouseClicked(MouseEvent mouseEvent) {
	//
	// panelX = width - 120;
	//
	// // if (mouseX > panelX){
	// // return;
	// // }
	//
	// for (MouseInteraction plugin : mouseInteractions) {
	// boolean b = plugin.mouseClicked(mouseX, mouseY,
	// easyMappingEditor.getNewThing(), mouseEvent);
	// if (b) {
	// return;
	// }
	// }
	//
	// }

	void controlEvent(ControlEvent theEvent) {

		// painterManager.controlEvent(theEvent);

		// for (GUIInteraction plugin : controlP5Interactions) {
		//
		// try {
		// plugin.controlEvent(theEvent);
		// } catch (Exception e) {
		// e.printStackTrace();
		// System.out.println("ERROR EN EL PLUGIN " + plugin.toString());
		// }
		// }
	}

	// public void startVisual() {
	// for (EasyMappingPlugin plugin : plugins) {
	// plugin.start(world, this, controlManager);
	// }
	// }
	//
	// public void endVisual() {
	// for (EasyMappingPlugin plugin : plugins) {
	// plugin.stop(world, this, controlManager);
	// }
	// }

	// public void keyReleased() {
	// // TODO Auto-generated method stub
	// keyBoardInteraction.keyReleased(this);
	// }
	//
	// public void keyPressed() {
	// keyBoardInteraction.keyPressed(this);
	// println(KeyEvent.getKeyText(keyCode));
	//
	// for (MouseInteraction mouseInteraction : mouseInteractions) {
	// mouseInteraction.KeyPressed(keyBoardInteraction);
	// }
	//
	// Thing newThing = easyMappingEditor.getNewThing();
	//
	// if (newThing != null)
	// for (MouseInteraction mouseInteraction : mouseInteractions) {
	// mouseInteraction.editKeyPressed(key, newThing);
	// }
	//
	// if (key == 'p') {
	// saveToFile();
	// }
	//
	// if (key == 'l') {
	// loadFromFile();
	// }
	//
	// if (key == 'e' || key == 'E') {
	// editDraw = !editDraw;
	// }
	// }

	static public void main(String args[]) {
		PApplet.main(new String[] { /*"--present", */"--bgcolor=#666666",
				"--stop-color=#cccccc",
				"edu.maping.easy.model.PolygonsByClickSecond" });
	}
}
